﻿using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessObject;
using BusinessLogic;
using Utility;
using System.Web.Security;
using System.IO;
namespace WebTeamplate
{
    public partial class Admin_User : BasePage
    {
        public string paramater
        {
            get
            {
                object o = ViewState["paramater"];
                if (o != null) return (string)o;
                return string.Empty;
            }
            set { ViewState["paramater"] = value; }
        }
        public string urlDetail = "UserDetail.aspx";
        #region ViewState
        public int F_PageIndex
        {
            get
            {
                object o = ViewState["F_PageIndex"];
                if (o != null) return (int)o;
                return 0;
                //a default

            }
            set { ViewState["F_PageIndex"] = value; }

        }
        public int F_PageSize
        {
            get
            {
                object o = ViewState["F_PageSize"];
                if (o != null) return (int)o;
                return 0;
                //a default

            }
            set { ViewState["F_PageSize"] = value; }

        }
        public string F_SortField
        {

            get
            {
                object o = ViewState["F_SortField"];
                if (o != null) return (string)o;
                return string.Empty;
            }

            set { ViewState["F_SortField"] = value; }

        }
        public string F_SortExpression
        {

            get
            {
                object o = ViewState["F_SortExpression"];
                if (o != null) return (string)o;
                return string.Empty;
            }

            set { ViewState["F_SortExpression"] = value; }

        }
        #endregion
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            InitTextControl();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            ClientDefaultButton.SetPageDefaultButton(this.Page, btnSearch.ClientID);
            if (!Page.IsPostBack)
            {

                CommonFunction.LoadSubCate(F_drlGender, (int)SystemEnum.Category.Gender, true);
                F_txtUserName.Focus();
                InitData();
                LoadData();
            }
        }
        private int maxRole = 0;
        int count = 0;
        public void LoadData()
        {
            paramater = this.GetPageParams(FilterFieldType.All);
            Vw_aspnet_Roles objRole = UserProfileUtil.GetMaxRole(this.userloginId, this.applicationName);
            if (objRole != null)
                maxRole = objRole.Level;
            hidListId.Value = string.Empty;
            hidListIdSelect.Value = string.Empty;
            UserProfile objUser = new UserProfile();
            //WHERE dbo.isGetUserForSupperAdmin(''' + @supperAdmin + ''',u.UserId)=1
            objUser.ApplicationName = this.applicationName;
            string condition = " where 1=1 ";
            if (F_txtEmail.Text != string.Empty)
                condition += " and m.Email like N'%" + F_txtEmail.Text + "%'";
            if (F_drlGender.SelectedValue != string.Empty)
                condition += " and p.Gender=" + F_drlGender.SelectedValue;
            if (F_txtName.Text != string.Empty)
                condition += " and p.Name like N'%" + F_txtName.Text + "%' ";
            if (F_rblStatus.SelectedValue != "")
                condition += " and m.[IsApproved]=" + F_rblStatus.SelectedValue;

            objUser.Condition = condition;
            objUser.ObjectPaging.PageIndex = this.F_PageIndex;
            objUser.ObjectPaging.PageSize = F_PageSize;
            objUser.ObjectSortOrder.SortExpression = this.F_SortExpression;
            objUser.ObjectSortOrder.SortField = this.F_SortField;

            List<UserProfileResult> lstResult = UserProfileUtil.SearchUserProfileSomeCondition(objUser);
            if (lstResult != null && lstResult.Count > 0)
                count = lstResult.Count;
            rptData.DataSource = lstResult;
            rptData.DataBind();
            pagerTop.SetPaging(this.F_PageIndex, this.F_PageSize, count, objUser.ToltalRow);
            pagerBottom.SetPaging(this.F_PageIndex, this.F_PageSize, count, objUser.ToltalRow);
            pagerBottom.Visible = false;
            pagerTop.Visible = false;
        }
        public void InitTextControl()
        {
            btnSearch.Text = Resources.Resource.SearchButtonText;
            btnReset.Text = Resources.Resource.ResetButtonText;
            btnAddNew.Text = Resources.Resource.AddNewButtonText;
            btnDelete.Text = Resources.Resource.DeleteText;
        }
        private void InitData()
        {

            this.F_PageIndex = ConvertValueObject.ParseInt(CommonFunction.GetValueFromQueryString(this.Page, "F_PageIndex"));
            if (F_PageIndex < 1)
                F_PageIndex = 1;
            this.F_PageSize = ConvertValueObject.ParseInt(CommonFunction.GetValueFromQueryString(this.Page, "F_PageSize"));
            if (F_PageSize < 1)
                F_PageSize = CommonFunction.AdminPageSize;
            F_SortExpression = CommonFunction.GetValueFromQueryString(this.Page, "F_SortExpression");
            if (F_SortExpression == string.Empty)
                F_SortExpression = "ASC";
            F_SortField = CommonFunction.GetValueFromQueryString(this.Page, "F_SortField");
            if (F_SortField == string.Empty)
                this.F_SortField = "[Name]";

            this.F_txtName.Text = CommonFunction.GetValueFromQueryString(this.Page, "F_txtName");
            this.F_txtUserName.Text = CommonFunction.GetValueFromQueryString(this.Page, "F_txtUserName");
            this.F_txtEmail.Text = CommonFunction.GetValueFromQueryString(this.Page, "F_txtEmail");
            this.F_rblStatus.Text = CommonFunction.GetValueFromQueryString(this.Page, "F_rblStatus");
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.F_PageIndex = 1;
            LoadData();
        }
        protected void rptData_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                UserProfileResult cate = (UserProfileResult)e.Item.DataItem;
                if (cate == null)
                    return;
                MembershipUser member = Membership.GetUser(cate.UserId);
                Vw_aspnet_Roles objRoleMax = UserProfileUtil.GetMaxRole(cate.UserId, this.ApplicationName);
                int level = 0;
                try
                {
                    level = objRoleMax.Level;
                }
                catch
                { }
                if (level > maxRole)
                {
                    e.Item.Visible = false;
                    return;
                }

                Literal ltr = (Literal)e.Item.FindControl("ltrNo");
                ltr.Text = (e.Item.ItemIndex + 1).ToString();

                //ltr = (Literal)e.Item.FindControl("ltrSubCode");
                string url = urlDetail + "?" + paramater + "&id=" + cate.UserId.ToString() + "&act=" + CommonFunction.EdityCommand;

                //ltr.Text = "<a href='" + url + "'>" + Server.HtmlEncode(cate.SubCategoryCode) + "</a>";
                ltr = (Literal)e.Item.FindControl("ltrName");
                ltr.Text = "<a href='" + url + "'>" + Server.HtmlEncode(cate.Name) + "</a>";
                ltr = (Literal)e.Item.FindControl("ltrUserName");
                ltr.Text = member.UserName;
                ltr = (Literal)e.Item.FindControl("ltrEmail");
                ltr.Text = member.Email;

                ImageButton imgButton = (ImageButton)e.Item.FindControl("btnPublish");
                imgButton.CommandArgument = cate.UserId.ToString();
                if (member.IsApproved == true)
                    imgButton.ImageUrl = CommonFunction.ActiveImage;
                else
                    imgButton.ImageUrl = CommonFunction.DeActiveImage;


                HyperLink hplEdit = (HyperLink)e.Item.FindControl("hplEdit");
                hplEdit.NavigateUrl = url;
                hplEdit.Text = Resources.Resource.EditText;
                LinkButton lbtnDelete = (LinkButton)e.Item.FindControl("lbtnDelete");
                lbtnDelete.Text = Resources.Resource.DeleteText;
                lbtnDelete.CommandArgument = cate.UserId.ToString();
                hidListId.Value += cate.UserId.ToString() + ";";
                if (cate.UserId == this.userloginId)
                {
                    lbtnDelete.Visible = false;
                    imgButton.Enabled = false;
                }
            }
        }


        protected void btnAddNew_Click(object sender, EventArgs e)
        {
            Response.Redirect(urlDetail + "?" + paramater);
        }
        protected void btnDelete_Click(object sender, EventArgs e)
        {

        }
        private void Delete(string id)
        {
            MembershipUser user = Membership.GetUser(new Guid(id));
            if (user != null)
            {
                if (Membership.DeleteUser(user.UserName))
                {
                    //delete Profile
                    UserProfileUtil.Delete(new Guid(user.ProviderUserKey.ToString()));
                    LoadData();
                }
            }
        }
        public void ActiveUser(string id)
        {
            MembershipUser user = Membership.GetUser(new Guid(id));
            if (user != null)
            {
                if (user.IsApproved)
                    user.IsApproved = false;
                else
                    user.IsApproved = true;
                Membership.UpdateUser(user);
                LoadData();
            }


        }


        protected void rptData_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            string id = e.CommandArgument.ToString();
            switch (e.CommandName)
            {
                case "Approved":
                    {
                        ActiveUser(id);
                        break;
                    }
                case "delete":
                    {
                        Delete(id);
                        break;
                    }

            }
        }
    }
}
